package com.turing.post.report.util;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;

import com.turing.post.util.PCPSUtil;

/**
 * 凭证库存日结表
 * 
 * @author yxb
 * 
 */
@SuppressWarnings("serial")
@Entity
@Table(name = "VoucherStockDaySettlement")
@IdClass(VoucherStockDaySettlement.class)
public class VoucherStockDaySettlement implements Serializable {

	/**
	 * 机构号
	 */
	private String branchNo;

	/**
	 * 结算日期
	 */
	private Date tranDate;

	/**
	 * 凭证种类ID
	 */
	private BigInteger voucherId;

	/**
	 * 发卡商No
	 */
	private String issueCompanyNo;

	/**
	 * 上日结余
	 */
	private BigInteger lastBalance;

	/**
	 * 采购入库
	 */
	private BigInteger buyIn;

	/**
	 * 调拨入库
	 */
	private BigInteger transferIn;

	/**
	 * 盘盈入库
	 */
	private BigInteger surplusIn;

	/**
	 * 调拨出库
	 */
	private BigInteger transferOut;

	/**
	 * 业务使用
	 */
	private BigInteger businessOut;

	/**
	 * 丢失损坏出库
	 */
	private BigInteger lossDamageOut;

	/**
	 * 本日结余
	 */
	private BigInteger balance;

	@Id
	@Column(name = "BranchNo", nullable = false)
	public String getBranchNo() {
		return branchNo;
	}

	public void setBranchNo(String branchNo) {
		this.branchNo = branchNo;
	}

	@Id
	@Column(name = "TranDate", nullable = false)
	public Date getTranDate() {
		return tranDate;
	}

	public void setTranDate(Date tranDate) {
		this.tranDate = tranDate;
	}

	@Id
	@Column(name = "VoucherId", nullable = false)
	public BigInteger getVoucherId() {
		return voucherId;
	}

	public void setVoucherId(BigInteger voucherId) {
		this.voucherId = voucherId;
	}

	@Column(name = "IssueCompanyNo", nullable = false)
	public String getIssueCompanyNo() {
		return issueCompanyNo;
	}

	public void setIssueCompanyNo(String issueCompanyNo) {
		this.issueCompanyNo = issueCompanyNo;
	}

	@Column(name = "LastBalance", nullable = false)
	public BigInteger getLastBalance() {
		return lastBalance;
	}

	public void setLastBalance(BigInteger lastBalance) {
		this.lastBalance = lastBalance;
	}

	@Column(name = "BuyIn", nullable = false)
	public BigInteger getBuyIn() {
		return buyIn;
	}

	public void setBuyIn(BigInteger buyIn) {
		this.buyIn = buyIn;
	}

	@Column(name = "TransferIn", nullable = false)
	public BigInteger getTransferIn() {
		return transferIn;
	}

	public void setTransferIn(BigInteger transferIn) {
		this.transferIn = transferIn;
	}

	@Column(name = "SurplusIn", nullable = false)
	public BigInteger getSurplusIn() {
		return surplusIn;
	}

	public void setSurplusIn(BigInteger surplusIn) {
		this.surplusIn = surplusIn;
	}

	@Column(name = "TransferOut", nullable = false)
	public BigInteger getTransferOut() {
		return transferOut;
	}

	public void setTransferOut(BigInteger transferOut) {
		this.transferOut = transferOut;
	}

	@Column(name = "BusinessOut", nullable = false)
	public BigInteger getBusinessOut() {
		return businessOut;
	}

	public void setBusinessOut(BigInteger businessOut) {
		this.businessOut = businessOut;
	}

	@Column(name = "LossDamageOut", nullable = false)
	public BigInteger getLossDamageOut() {
		return lossDamageOut;
	}

	public void setLossDamageOut(BigInteger lossDamageOut) {
		this.lossDamageOut = lossDamageOut;
	}

	@Column(name = "Balance", nullable = false)
	public BigInteger getBalance() {
		return balance;
	}

	public void setBalance(BigInteger balance) {
		this.balance = balance;
	}

	/**
	 * 重新计算本日余额
	 */
	public void reCalcBalance() {
		BigInteger bal = getLastBalance();
		bal = bal.add(getBuyIn());
		bal = bal.add(getTransferIn());
		bal = bal.add(getSurplusIn());
		bal = bal.subtract(getBusinessOut());
		bal = bal.subtract(getTransferOut());
		bal = bal.subtract(getLossDamageOut());
		setBalance(bal);
	}

	@Override
	public int hashCode() {
		return PCPSUtil.getObjectListHashCode(new Object[] { getBranchNo(),
				getTranDate(), getVoucherId() });
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof VoucherStockDaySettlement))
			return false;

		VoucherStockDaySettlement other = (VoucherStockDaySettlement) obj;

		if (!PCPSUtil.compareObject(this.getBranchNo(), other.getBranchNo()))
			return false;

		if (!PCPSUtil.compareObject(this.getTranDate(), other.getTranDate()))
			return false;

		return PCPSUtil
				.compareObject(this.getVoucherId(), other.getVoucherId());
	}
}